/**
 * 
 */
$(document).ready(function(){
	 $.ajax({
		 	url:"/getSession",
			type:"post",	
			data: JSON.stringify({"code":0}),
		    dataType:'json',
		    contentType:'application/json;charset=UTF-8',
			success:function(data){
				userIdentifier = data.identifier;
				if(userIdentifier == "404"){
					window.location.href="https://fuwu.baidu.com/";
					return;
				}
			}
	 })
});

$(function () {
	$(".fied").show();
	var carId = 1;
	var requestType=1;
    var userIdentifier;
    $.ajax({
    	url:"/getSession",
	    type:"post",	
	    data: JSON.stringify({"code":0}),
	    dataType:'json',
	    contentType:'application/json;charset=UTF-8',
	    success:function(data){
			userIdentifier = data.identifier;
			if(userIdentifier == "404"){
    			alert("登录时间已超时，请重新从百度应用界面打开!");
    			$(".fied").hide();
				return;
			}
		    $.ajax({
		        url: "/ideaController/getUserIdeaList?identifier="+userIdentifier+"&page="+carId+"&requestType="+requestType,
		        datatype: 'json',
		        type: "post",
			    contentType:'application/json;charset=UTF-8',
		        success: function (data) {
		            if (data != null&&data.pageCount!=0){
		            	var tbody=$('<tbody></tbody>');
		                $.each(eval(data).userIdeaList,function (index, item) { //遍历返回的json
		                	var tr=$('<tr></tr>');
		                	tr.append('<td class="one_td"><span class="checkbox-input_left js-checkbox-list-input"><input type="checkbox" name="test" value="'+ item.id +'"/></span></td>');
		                	tr.append(' <td class="two"> <span class="creative_content"><span class="describe-one js-one">'
		         					+item.ideaTitle+'</span><span class="describe-two">'
		         					+item.ideaDesOne+'</span><span class="describe-two">'
		         					+item.ideaDesTwo+'</span><span class="describe-url">'
		         					+item.displayUrl+'</span></span></td>');
		                	var idea = JSON.stringify(item);
		         			var $td = $('<td class="three"></td>');
		         			var $span = $('<span class="list_compile"></span>');
		         			var $img = $('<img data-origin='+ idea +' src="../img/pencil.png"/>');
		         			
		         			$img.click(function(){
		         				
		         				var idea = $(this).data('origin');
		         				$(".js-open").show();
		         				$(".js-open").find(".msgid").val(idea.id);
		         				$(".js-open").find(".msgUnitid").val(idea.adgroupId);
                 				$(".js-open").find(".msgCreativeId").val(idea.creativeId);
		         				// 标题
		         				$(".js-open").find(".js-title").val(idea.ideaTitle);
		         				$(".js-open").find(".js-number-title").text(idea.ideaTitle.length);
		         				// 描述1 
		         			 	$(".js-open").find(".js-content-one").val(idea.ideaDesOne);
		         				$(".js-open").find(".js-content-one-munber").text(idea.ideaDesOne.length);
		         				// 描述2
		         				$(".js-open").find(".js-content-tow").val(idea.ideaDesTwo);
		         				$(".js-open").find(".js-content-two-munber").text(idea.ideaDesTwo.length);
		         				// 显示网址
		         				$(".js-open").find(".js-content-url").val(idea.displayUrl);
		         				$(".js-open").find(".js-show-url-munber").text(idea.displayUrl.length);
		         				// 访问网址
		         				$(".js-open").find(".js-content-visit").val(idea.visitUrl);
		         				$(".js-open").find(".js-content-three").text(idea.visitUrl.length); 
		         				
		         			})
		         			$span.append($img);
		         			$td.append($span);
		         			tr.append($td);
		                	tbody.append(tr);
		                });
		                 $('#test_table tbody').replaceWith(tbody);

		                var pageCount = eval(data ).pageCount; //取到pageCount的值(把返回数据转成object类型)
		                var currentPage = eval(data ).page; //得到urrentPage
		                var options = {
		                    bootstrapMajorVersion: 2, //版本
		                    currentPage: currentPage, //当前页数
		                    totalPages: pageCount, //总页数
		                    itemTexts: function (type, page, current) {
		                        switch (type) {
		                            case "first":
		                                return "首页";
		                            case "prev":
		                                return "<<";
		                            case "next":
		                                return ">>";
		                            case "last":
		                                return "末页";
		                            case "page":
		                                return page;
		                        }
		                    },//点击事件，用于通过Ajax来刷新整个list列表
		                    onPageClicked: function (event, originalEvent, type, page) {
		                    	 $.ajax({
		                    	 	url:"/getSession",
			         		type:"post",	
			         		data: JSON.stringify({"code":0}),
			         		dataType:'json',
			         		contentType:'application/json;charset=UTF-8',
			         		success:function(data){
		                    				userIdentifier = data.identifier;
		                    				if(userIdentifier == "404"){
		                    	    			alert("登录时间已超时，请重新从百度应用界面打开!");
		                    					return;
		                    				}
				                    	    $.ajax({
					                            url: "/ideaController/getUserIdeaList?identifier="+userIdentifier+"&requestType="+requestType+"&page=" + page,
					                            type: "Post",
					                            success: function (data1) {
					                                if (data1 != null&&data1.pageCount!=0){
					                                	var tbody=$('<tbody></tbody>');
					                                    $.each(eval(data1).userIdeaList, function (index, item) { //遍历返回的json
					                                    	var tr=$('<tr></tr>');
					                                    	tr.append('<td class="one_td"><span class="checkbox-input_left js-checkbox-list-input"><input type="checkbox" name="test" value="'+ item.id +'"/></span></td>');
					                                    	tr.append(' <td class="two"> <span class="creative_content"><span class="describe-one js-one">'
					                             					+item.ideaTitle+'</span><span class="describe-two">'
					                             					+item.ideaDesOne+'</span><span class="describe-two">'
					                             					+item.ideaDesTwo+'</span><span class="describe-url">'
					                             					+item.displayUrl+'</span></span></td>');
					                                    	var idea = JSON.stringify(item);
					                             			var $td = $('<td class="three"></td>');
					                             			var $span = $('<span class="list_compile"></span>');
					                             			var $img = $('<img data-origin='+ idea +' src="../img/pencil.png"/>');
					                             			
					                             			$img.click(function(){
					                             				
					                             				var idea = $(this).data('origin');
					                             				$(".js-open").show();
					                             				$(".js-open").find(".msgid").val(idea.id);
					                             				$(".js-open").find(".msgUnitid").val(idea.adgroupId);
					                             				$(".js-open").find(".msgCreativeId").val(idea.creativeId);
					                             				// 标题
					                             				$(".js-open").find(".js-title").val(idea.ideaTitle);
					                             				$(".js-open").find(".js-number-title").text(idea.ideaTitle.length);
					                             				// 描述1 
					                             			 	$(".js-open").find(".js-content-one").val(idea.ideaDesOne);
					                             				$(".js-open").find(".js-content-one-munber").text(idea.ideaDesOne.length);
					                             				// 描述2
					                             				$(".js-open").find(".js-content-tow").val(idea.ideaDesTwo);
					                             				$(".js-open").find(".js-content-two-munber").text(idea.ideaDesTwo.length);
					                             				// 显示网址
					                             				$(".js-open").find(".js-content-url").val(idea.displayUrl);
					                             				$(".js-open").find(".js-show-url-munber").text(idea.displayUrl.length);
					                             				// 访问网址
					                             				$(".js-open").find(".js-content-visit").val(idea.visitUrl);
					                             				$(".js-open").find(".js-content-three").text(idea.visitUrl.length); 
					                             			})
					                             			
					                             			$span.append($img);
					                             			$td.append($span);
					                             			tr.append($td);
					                                    	tbody.append(tr);
					                                    });
					                                    $('#test_table tbody').replaceWith(tbody);
					                                    var pageCount = eval(data1 ).pageCount; //取到pageCount的值(把返回数据转成object类型)
					                                    var currentPage = eval(data1 ).page; //得到urrentPage
					                                    var options = {
					                                            bootstrapMajorVersion: 2, //版本
					                                            currentPage: currentPage, //当前页数
					                                            totalPages: pageCount, //总页数
					                                            itemTexts: function (type, page, current) {
					                                                switch (type) {
					                                                    case "first":
					                                                        return "首页";
					                                                    case "prev":
					                                                        return "<<";
					                                                    case "next":
					                                                        return ">>";
					                                                    case "last":
					                                                        return "末页";
					                                                    case "page":
					                                                        return page;
					                                                }
					                                            }
					                                    };
					                                    $('#example').bootstrapPaginator(options);
					                                    $(".fied").hide();
					                                }else{
					                                	var tbody=$('<tbody></tbody>');
					            		            	var tr=$('<tr></tr>');
					            		            	tr.append('<td><span class="tab-self-motion-creative">您的账户还未创意，请您添加</span></td>');
					            		            	tbody.append(tr);
					            		            	$('#test_table tbody').replaceWith(tbody);
					            		            	$('#example').html("");
					            		            	$(".fied").hide();
					                                }
					                            },error:function(){
					                            	$(".fied").hide();
					                            }
					                        });
		                    			}
		                    	 });
		                    }
		                };
		                $('#example').bootstrapPaginator(options);
		                $(".fied").hide();
		            }else{
		            	var tbody=$('<tbody></tbody>');
		            	var tr=$('<tr></tr>');
		            	tr.append('<td><span class="tab-self-motion-creative">您的账户还未创意，请您添加</span></td>');
		            	tbody.append(tr);
		            	$('#test_table tbody').replaceWith(tbody);
		            	$('#example').html("");
		            	$(".fied").hide();
                    }
		        }
		    });
		},error:function(){
			$(".fied").hide();
		}
	}); 
});



$(function(){
	
	$(".js-download").click(function(){
		var delNum=new Array();
		var i=-1;
        $("input[name='test']:checked").each(function() { // 遍历选中的checkbox
        	n = $(this).parents("tr").index();  // 获取checkbox所在行的顺序
        	var code = $(this).parents("tr").find("[name='test']").val();  
        	delNum[++i]=code;
        }); 
        if(delNum.length <=0){
        	alert("请选中您要下载的信息，再点击下载");
        	return;
        }
        var url = "/ideaController/exportCreativeCSV";
        var form = $("<form></form>").attr("action", url).attr("method", "post");
        form.append($("<input></input>").attr("type", "hidden").attr("name", "fileName").attr("value", delNum));
        form.appendTo('body').submit().remove();
	})

	//切换图标颜色
    $("#tab-nav li").click(function(){
		$(this).addClass("selected-bule").siblings("li").removeClass("selected-bule");
		$(this).parent().parent().find('.content_module').eq($(this).index()).addClass("on").siblings().removeClass("on");
		$(".fied").show();
        var requestType;
        if($(".tab-self-motion").parent().attr("class").indexOf("selected-bule")>=0){
        	requestType=1;
        }else if($(".tab-manual").parent().attr("class").indexOf("selected-bule")>=0){
        	requestType=0;
        }
        var carId = 1;
        var userIdentifier;
        $.ajax({
        	url:"/getSession",
    	    type:"post",	
    	    data: JSON.stringify({"code":0}),
    	    dataType:'json',
    	    contentType:'application/json;charset=UTF-8',
    	    success:function(data){
    			userIdentifier = data.identifier;
    			if(userIdentifier == "404"){
        			alert("登录时间已超时，请重新从百度应用界面打开!");
        			$(".fied").hide();
    				return;
    			}
    		    $.ajax({
    		        url: "/ideaController/getUserIdeaList?identifier="+userIdentifier+"&page="+carId+"&requestType="+requestType,
    		        datatype: 'json',
    		        type: "post",
    			    contentType:'application/json;charset=UTF-8',
    		        success: function (data) {
    		        	if(data!=null&&data.pageCount!=0){
    		            	var tbody=$('<tbody></tbody>');
    		                $.each(eval(data).userIdeaList,function (index, item) { //遍历返回的json
    		                	var tr=$('<tr></tr>');
    		                	tr.append('<td class="one_td"><span class="checkbox-input_left js-checkbox-list-input"><input type="checkbox" name="test" value="'+ item.id +'"/></span></td>');
    		                	tr.append(' <td class="two"> <span class="creative_content"><span class="describe-one js-one">'
    		         					+item.ideaTitle+'</span><span class="describe-two">'
    		         					+item.ideaDesOne+'</span><span class="describe-two">'
    		         					+item.ideaDesTwo+'</span><span class="describe-url">'
    		         					+item.displayUrl+'</span></span></td>');
    		                	var idea = JSON.stringify(item);
    		         			var $td = $('<td class="three"></td>');
    		         			var $span = $('<span class="list_compile"></span>');
    		         			var $img = $('<img data-origin='+ idea +' src="../img/pencil.png"/>');
    		         			
    		         			$img.click(function(){
    		         				
    		         				
    		         				var idea = $(this).data('origin');
    		         				$(".js-open").show();
    		         				$(".js-open").find(".msgid").val(idea.id);
    		         				$(".js-open").find(".msgUnitid").val(idea.adgroupId);
                     				$(".js-open").find(".msgCreativeId").val(idea.creativeId);
    		         				// 标题
    		         				$(".js-open").find(".js-title").val(idea.ideaTitle);
    		         				$(".js-open").find(".js-number-title").text(idea.ideaTitle.length);
    		         				// 描述1 
    		         			 	$(".js-open").find(".js-content-one").val(idea.ideaDesOne);
    		         				$(".js-open").find(".js-content-one-munber").text(idea.ideaDesOne.length);
    		         				// 描述2
    		         				$(".js-open").find(".js-content-tow").val(idea.ideaDesTwo);
    		         				$(".js-open").find(".js-content-two-munber").text(idea.ideaDesTwo.length);
    		         				// 显示网址
    		         				$(".js-open").find(".js-content-url").val(idea.displayUrl);
    		         				$(".js-open").find(".js-show-url-munber").text(idea.displayUrl.length);
    		         				// 访问网址
    		         				$(".js-open").find(".js-content-visit").val(idea.visitUrl);
    		         				$(".js-open").find(".js-content-three").text(idea.visitUrl.length); 
    		         				
    		         			})
    		         			$span.append($img);
    		         			$td.append($span);
    		         			tr.append($td);
    		                	tbody.append(tr);
    		                });
    		                 $('#test_table tbody').replaceWith(tbody);

    		                var pageCount = eval(data ).pageCount; //取到pageCount的值(把返回数据转成object类型)
    		                var currentPage = eval(data ).page; //得到urrentPage
    		                var options = {
    		                    bootstrapMajorVersion: 2, //版本
    		                    currentPage: currentPage, //当前页数
    		                    totalPages: pageCount, //总页数
    		                    itemTexts: function (type, page, current) {
    		                        switch (type) {
    		                            case "first":
    		                                return "首页";
    		                            case "prev":
    		                                return "<<";
    		                            case "next":
    		                                return ">>";
    		                            case "last":
    		                                return "末页";
    		                            case "page":
    		                                return page;
    		                        }
    		                    },//点击事件，用于通过Ajax来刷新整个list列表
    		                    onPageClicked: function (event, originalEvent, type, page) {
    		                    	 $.ajax({
    		                    	 	url:"/getSession",
    			         		type:"post",	
    			         		data: JSON.stringify({"code":0}),
    			         		dataType:'json',
    			         		contentType:'application/json;charset=UTF-8',
    			         		success:function(data){
    		                    				userIdentifier = data.identifier;
    		                    				if(userIdentifier == "404"){
    		                    	    			alert("登录时间已超时，请重新从百度应用界面打开!");
    		                    					return;
    		                    				}
    				                    	    $.ajax({
    					                            url: "/ideaController/getUserIdeaList?identifier="+userIdentifier+"&requestType="+requestType+"&page="+page,
    					                            type: "Post",
    					                            success: function (data1) {
    					                                if (data1 != null&&data1.pageCount!=0) {
    					                                	var tbody=$('<tbody></tbody>');
    					                                    $.each(eval(data1).userIdeaList, function (index, item) { //遍历返回的json
    					                                    	var tr=$('<tr></tr>');
    					                                    	tr.append('<td class="one_td"><span class="checkbox-input_left js-checkbox-list-input"><input type="checkbox" name="test" value="'+ item.id +'"/></span></td>');
    					                                    	tr.append(' <td class="two"> <span class="creative_content"><span class="describe-one js-one">'
    					                             					+item.ideaTitle+'</span><span class="describe-two">'
    					                             					+item.ideaDesOne+'</span><span class="describe-two">'
    					                             					+item.ideaDesTwo+'</span><span class="describe-url">'
    					                             					+item.displayUrl+'</span></span></td>');
    					                                    	var idea = JSON.stringify(item);
    					                             			var $td = $('<td class="three"></td>');
    					                             			var $span = $('<span class="list_compile"></span>');
    					                             			var $img = $('<img data-origin='+ idea +' src="../img/pencil.png"/>');
    					                             			
    					                             			$img.click(function(){
    					                             				
    					                             				var idea = $(this).data('origin');
    					                             				$(".js-open").show();
    					                             				$(".js-open").find(".msgid").val(idea.id);
    					                             				$(".js-open").find(".msgUnitid").val(idea.adgroupId);
    					                             				$(".js-open").find(".msgCreativeId").val(idea.creativeId);
    					                             				// 标题
    					                             				$(".js-open").find(".js-title").val(idea.ideaTitle);
    					                             				$(".js-open").find(".js-number-title").text(idea.ideaTitle.length);
    					                             				// 描述1 
    					                             			 	$(".js-open").find(".js-content-one").val(idea.ideaDesOne);
    					                             				$(".js-open").find(".js-content-one-munber").text(idea.ideaDesOne.length);
    					                             				// 描述2
    					                             				$(".js-open").find(".js-content-tow").val(idea.ideaDesTwo);
    					                             				$(".js-open").find(".js-content-two-munber").text(idea.ideaDesTwo.length);
    					                             				// 显示网址
    					                             				$(".js-open").find(".js-content-url").val(idea.displayUrl);
    					                             				$(".js-open").find(".js-show-url-munber").text(idea.displayUrl.length);
    					                             				// 访问网址
    					                             				$(".js-open").find(".js-content-visit").val(idea.visitUrl);
    					                             				$(".js-open").find(".js-content-three").text(idea.visitUrl.length); 
    					                             			})
    					                             			
    					                             			$span.append($img);
    					                             			$td.append($span);
    					                             			tr.append($td);
    					                                    	tbody.append(tr);
    					                                    });
    					                                    $('#test_table tbody').replaceWith(tbody);
    					                                    var pageCount = eval(data1 ).pageCount; //取到pageCount的值(把返回数据转成object类型)
    					                                    var currentPage = eval(data1 ).page; //得到urrentPage
    					                                    var options = {
    					                                            bootstrapMajorVersion: 2, //版本
    					                                            currentPage: currentPage, //当前页数
    					                                            totalPages: pageCount, //总页数
    					                                            itemTexts: function (type, page, current) {
    					                                                switch (type) {
    					                                                    case "first":
    					                                                        return "首页";
    					                                                    case "prev":
    					                                                        return "<<";
    					                                                    case "next":
    					                                                        return ">>";
    					                                                    case "last":
    					                                                        return "末页";
    					                                                    case "page":
    					                                                        return page;
    					                                                }
    					                                            }
    					                                    };
    					                                    $('#example').bootstrapPaginator(options);
    					                                    $(".fied").hide();
    					                                }else{
    					                                	var tbody=$('<tbody></tbody>');
    					                                	var tr=$('<tr></tr>');
    					                                	tr.append('<td><span class="tab-self-motion-creative">您的账户还未创意，请您添加</span></td>');
    					                                	tr.append($td);
    					                                	tbody.append(tr);
    					                                	$('#test_table tbody').replaceWith(tbody);
    					                                	$('#example').html("");
    					                                	$(".fied").hide();
    					                                }
    					                            },error:function(){
    					            		        	$(".fied").hide();
    					            		        	
    					            		        }
    					                        });
    		                    			}
    		                    	 });
    		                    }
    		                };
    		                $('#example').bootstrapPaginator(options);
    		                $(".fied").hide();
    		            }else{
    		            	var tbody=$('<tbody></tbody>');
    		            	var tr=$('<tr></tr>');
    		            	tr.append('<td><span class="tab-self-motion-creative">您的账户还没有创意，请您添加</span></td>');
    		            	tbody.append(tr);
    		            	$('#test_table tbody').replaceWith(tbody);
    		            	$('#example').html("");
    		            	$(".fied").hide();
    		            }
    		        },error:function(){
    		        	$(".fied").hide();
    		        }
    		    });
    		}
    	}); 
        
    })
	
	
	// 点击选择
	$(".js-checkbox-list-input input").click(function(){
		var whetherCheckbox = $(this).attr('checked');
		if (whetherCheckbox == 'checked') {
			$(this).prop('checked',true);
		} else {
			$(this).prop('checked',false);
			$(".js-check").attr("checked",false);
		}
	});
	
	//全选或者全不选
	$(".js-check").click(function(){ 
		for (var i = 0; i < $(".js-checkbox-list-input input").length; i++) {
			if ($(".js-checkbox-list-input input")[i].checked == true) {
				$(".js-checkbox-list-input input")[i].checked=false;
			} else {
				$(".js-checkbox-list-input input")[i].checked=true;
			}
		}
	});

	// 选中删除
	$(".js-download-del").click(function() {
		var delNum=new Array();
		var i=-1;
        $("input[name='test']:checked").each(function() { // 遍历选中的checkbox
        	n = $(this).parents("tr").index();  // 获取checkbox所在行的顺序
        	var code = $(this).parents("tr").find("[name='test']").val();  
        	delNum[++i]=code;
        }); 
        if(delNum.length <=0){
        	alert("请选中您要删除的信息，再点击删除");
        	return;
        }
        $(".fied").show();
        //利用对话框返回的值 （true 或者 false）  
        if (confirm("你确定删除这些创意吗？")) {  
        	var requestType;
            if($(".tab-self-motion").parent().attr("class").indexOf("selected-bule")>=0){
             	requestType=1;
            }else if($(".tab-manual").parent().attr("class").indexOf("selected-bule")>=0){
             	requestType=0;
            }
            var successIdArray=null;
            var isAllRemove = 0;
        	$.ajax({
    			url:"/getSession",
    			type:"post",	
    			data: JSON.stringify({"code":0}),
    		    dataType:'json',
    		   	contentType:'application/json;charset=UTF-8',
    		   	success:function(data){
    				var userIdentifier = data.identifier;
    				if(userIdentifier == "404"){
    					alert("登录时间已超时，请退出重新授权使用本应用");
    					return;
    				}
		        	$.ajax({
		         	    type: "POST",
		         	    url: "/ideaController/deleteMore",
		         	    data: JSON.stringify({"ids":delNum,"userId":userIdentifier,"requestType":requestType}),
		         	    dataType:'json',
		         	    contentType:'application/json;charset=UTF-8',
		         	    success: function(data){
/*		         	    	if(data.successWithFail!=null){
		         	    		alert( "删除成功+"可能是以下原因导致："+data.successWithFail.msg);
		         	    		successIdArray = data.successWithFail.failedArray
		         	    		return;
		         	    	}else if(data.code == 410){
		         	    		alert( "删除失败,错误信息:"+data.msg);
		         	    		isAllRemove = 0;
		         	    		return;
		         	    	}
		         	    	isAllRemove = 1;*/
		         	    	if(data.code == 410){
		         	    		console.log(data.msg);
		         	    	}
		            		$("input[name='test']:checked").each(function() { // 遍历选中的checkbox
		    	            	n = $(this).parents("tr").index();  // 获取checkbox所在行的顺序
		    /*	            	var flag = 0;
		    	            	for(var i=0;i<successIdArray.length;i++){
		    	            		if($(this).parents("tr").find("[name='test']").val() == successIdArray[i]){
		    	            			flag = 1;
		    	            			break;
		    	            		}
		    	            	}
		    	            	if(flag == 0){*/
		    	            		$("table#test_table").find("tr:eq("+n+")").remove();
		    	             })
		         			alert( "删除成功");
		         	    }
		        	});
    		   	}
        	});
        	$(".fied").hide();
/*        	if(successIdArray==null){
        		if(isAllRemove == 1){
        			 $("input[name='test']:checked").each(function() { // 遍历选中的checkbox
        	            	n = $(this).parents("tr").index();  // 获取checkbox所在行的顺序
        	             	$("table#test_table").find("tr:eq("+n+")").remove();
        	         }); 
        		}else{
        			return;
        		}
        	}else if(successIdArray!=null){*/

/*	         }); 
        	}*/ 
        } else {  
        	$(".fied").hide();
            return;
        }  
       
    });
	
	
	$(".save").click(function(){	
		var id  = $(".js-open").find(".msgid").val();
		var title = $(".js-open").find(".js-title").val();
		var content1 = $(".js-open").find(".js-content-one").val();
		var content2 = $(".js-open").find(".js-content-tow").val();
		var showUrl = $(".js-open").find(".js-content-url").val();
		var visitUrl = $(".js-open").find(".js-content-visit").val();
		if(title.length > 40||content1.length > 80||content2.length > 80||showUrl.length > 36||visitUrl.length > 1024){
			alert("信息长度超出范围，请检查后再保存");
			return;
		}else if(title.length <= 0||content1.length <= 0||content2.length <= 0||showUrl.length <= 0||visitUrl.length <= 0){
			alert("创意信息部分为空，请检查后再保存");
			return;
		}
		var userIdentifier;
		$.ajax({
			url:"/getSession",
			type:"post",	
			data: JSON.stringify({"code":0}),
		    	dataType:'json',
		   	contentType:'application/json;charset=UTF-8',
		   	success:function(data){
				userIdentifier = data.identifier;
				if(userIdentifier == "404"){
					alert("登录时间已超时，请退出重新授权使用本应用");
					return;
				}
				$.ajax({
			    type: "POST",
			    url: "/ideaController/update",
			    data: JSON.stringify({"id":id,"ideaTitle":title,"ideaDesOne":content1,"ideaDesTwo":content2,"displayUrl":showUrl,"visitUrl":visitUrl,"userId":userIdentifier}),
			    dataType:'json',
			    contentType:'application/json;charset=UTF-8',
			    success: function(){
					alert( "创意修改成功");
					window.location.href="save_creative.html";
			    }
				});
			}
			});
	});
	
	$(".upload").click(function(){
		$(".fied").show();
		var id  = $(".js-open").find(".msgid").val();
		var unitId  = $(".js-open").find(".msgUnitid").val();
		var creativeId  = $(".js-open").find(".msgCreativeId").val();
		var title = $(".js-open").find(".js-title").val();
		var content1 = $(".js-open").find(".js-content-one").val();
		var content2 = $(".js-open").find(".js-content-tow").val();
		var showUrl = $(".js-open").find(".js-content-url").val();
		var visitUrl = $(".js-open").find(".js-content-visit").val();
		var addOrUpdateFlag;
		if(creativeId==null||creativeId==""){
			creativeId = 0;
		}
		if(title.length > 40||content1.length > 80||content2.length > 80||showUrl.length > 36||visitUrl.length > 1024){
			alert("信息长度超出范围，请检查后再保存");
			return;
		}else if(title.length <= 0||content1.length <= 0||content2.length <= 0||showUrl.length <= 0||visitUrl.length <= 0){
			alert("创意信息部分为空，请检查后再保存");
			return;
		}
		var userIdentifier;
		$.ajax({
			url:"/getSession",
			type:"post",	
			data: JSON.stringify({"code":0}),
		    	dataType:'json',
		   	contentType:'application/json;charset=UTF-8',
		   	success:function(data){
				userIdentifier = data.identifier;
				if(userIdentifier == "404"){
					alert("登录时间已超时，请退出重新授权使用本应用");
					return;
				}
				$.ajax({
			    type: "POST",
			    url: "/ideaController/ideaOperation",
			    data: JSON.stringify({"id":id,"creativeId":creativeId,"adgroupId":unitId,"ideaTitle":title,"ideaDesOne":content1,"ideaDesTwo":content2,"displayUrl":showUrl,"visitUrl":visitUrl,"userId":userIdentifier,"code":1}),
			    dataType:'json',
			    contentType:'application/json;charset=UTF-8',
			    success: function(data){
			    	if(data.code == 410){
						alert("创意保存与上传成失败，可能是以下原因导致的："+data.msg);
						$(".fied").hide();
						return;
					}
			    	$(".fied").hide();
					alert( "创意修改成功");
					window.location.href="save_creative.html";
			    }
				});
			}
			});
	})
	
	
	
    // 关闭
	$(".js-delgrayImg").click(function(){
		$(".js-open").hide();
	});
	
	// 监听
	$(".js-title").on("input",function(){
		if($(this).val().length > 40){
			alert("超出长度了");
			$(".js-number-title").text($(this).val().length);
		} else {
			$(".js-number-title").text($(this).val().length);
		}
	})

	$(".js-content-one").on("input",function(){
		if ($(this).val().length > 80) {
			alert("超出长度了");
			$(".js-content-one-munber").text($(this).val().length);
		} else {
			$(".js-content-one-munber").text($(this).val().length);
		}
	})

	$(".js-content-tow").on("input",function(){
		if ($(this).val().length > 80) {
			alert("超出长度了");
			$(".js-content-two-munber").text($(this).val().length);
		} else {
			$(".js-content-two-munber").text($(this).val().length);
		}
	})

	$(".js-content-url").on("input",function(){
		if ($(this).val().length > 80) {
			alert("超出长度了");
			$(".js-show-url-munber").text($(this).val().length);
		} else {
			$(".js-show-url-munber").text($(this).val().length);
		}
	})

	$(".js-content-visit").on("input",function(){
		if ($(this).val().length > 1024) {
			alert("超出长度了");
			$(".js-content-three").text($(this).val().length);
		} else {
			$(".js-content-three").text($(this).val().length);
		}
	})
	function setIfOpen(val) {
		document.cookie = "ifOpen=" + val;
	}

	function getIfOpenVal(param) {
		var strCookie = document.cookie;
		var arrCookie = strCookie.split(";");
		for (var i = 0; i < arrCookie.length; i++) {
			var arr = arrCookie[i].split("=");
			if (arr[0] == param)
				return arr[1];
		}
		return null;
	}
	
	//收缩服务
	var ifOpen = getIfOpenVal("ifOpen");
	// index页面默认展开浮窗
	if(ifOpen == 0){
		$(".business-bridge-iframe").animate({
			left : "-230px",
		}, 0)
		$(".js-shrink-button").find("span").removeClass("control").text("服务指导");
			$(".js-shrink-button").toggle(function(){
				$(".business-bridge-iframe").animate({
					left : "0px",
				}, 1000)
				$(this).find("span").addClass("control").text("收起");
				setIfOpen(1);
		}, function() {
			$(".business-bridge-iframe").animate({
            left:"-230px",
        },1000)
        $(this).find("span").removeClass("control").text("服务指导");
			setIfOpen(0);
		})
	}else{
		$(".js-shrink-button").toggle(function() {
			$(".business-bridge-iframe").animate({
				left : "-230px",
			}, 1000)
			$(this).find("span").removeClass("control").text("服务指导");
			setIfOpen(0);
    },function(){
        $(".business-bridge-iframe").animate({
            left:"0px",
        },1000)
        $(this).find("span").addClass("control").text("收起");
        setIfOpen(1);
    	})
	}
    
     // 关闭浮窗
    $(".js-off-white").click(function(){
        $(".business-bridge-iframe").hide();
    })
    
});
